package com.qing.microservices.kafkaproducer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author guoqf
 */
@Slf4j
@Component
public class KafkaSenderMain {

    @Resource
    private KafkaSender kafkaSender;

    private int sendTimes = 10;
    
    @PostConstruct
    public void init() {
        sendBaseMessageInLoop();
    }

    private void sendBaseMessageInLoop() {
        int times = sendTimes;
        while (times > 0) {
            String message = "message" + times;
            log.info("To send msg [{}]", message);
            kafkaSender.send("baseMessage", message);
            times--;
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
